package com.cruise.h5.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cruise.common.web.util.WebUtils;
import com.cruise.h5.mapper.VideoCollectionMapper;
import com.cruise.h5.mapper.VideoMapper;
import com.cruise.h5.pojo.dto.CollectDTO;
import com.cruise.h5.pojo.dto.VideoCollectionListDTO;
import com.cruise.h5.pojo.entity.Video;
import com.cruise.h5.pojo.entity.VideoCollection;
import com.cruise.h5.pojo.vo.VideoListVO;
import com.cruise.h5.service.VideoCollectionService;
import com.cruise.system.userapi.entity.SysUser;
import com.cruise.system.userapi.feignapi.FeignSystemApi;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;

import javax.annotation.Resource;
import java.util.ArrayList;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author cruise
 * @since 2021-04-19
 */
@Service
@Transactional(rollbackFor = RuntimeException.class)
public class VideoCollectionServiceImpl extends ServiceImpl<VideoCollectionMapper, VideoCollection> implements VideoCollectionService {

    @Resource
    private VideoCollectionMapper videoCollectionMapper;

    @Resource
    private VideoMapper videoMapper;

    @Resource
    private FeignSystemApi feignSystemApi;


    @Override
    public Integer collect(@RequestBody CollectDTO collectDTO) {
        if(collectDTO.getIsCollect() > 0){
            // 添加收藏
            if(!isAlreadyCollect(collectDTO.getId())){
                VideoCollection videoCollection = new VideoCollection(WebUtils.getUserId() , collectDTO.getId());
                videoCollectionMapper.insert(videoCollection);
                videoMapper.addCollectCount( collectDTO.getId());
            }
        }else if(collectDTO.getIsCollect() < 0){
            // 移除收藏
            if(isAlreadyCollect(collectDTO.getId())){
                videoCollectionMapper.delete(new QueryWrapper<VideoCollection>().lambda().eq(VideoCollection::getUserId, WebUtils.getUserId()).eq(VideoCollection::getVideoId, collectDTO.getId()));
                videoMapper.cancelCollectCount(collectDTO.getId());
            }
        }
        Video video = videoMapper.selectById(collectDTO.getId());
        return video.getCollectCount();
    }

    private Boolean isAlreadyCollect(String videoId){
        VideoCollection videoCollection = videoCollectionMapper.selectOne(new QueryWrapper<VideoCollection>().lambda().eq(VideoCollection::getUserId, WebUtils.getUserId()).eq(VideoCollection::getVideoId, videoId));
        return videoCollection !=null ;
    }

    @Override
    public IPage<VideoListVO> getVideoCollectionList(VideoCollectionListDTO videoCollectionListDTO) {

        Page<VideoListVO> videoListVoPage = new Page<>();
        if(StringUtils.isEmpty(videoCollectionListDTO.getUserId())){
            videoCollectionListDTO.setUserId(WebUtils.getUserId());
        }
        Page<VideoCollection> videoCollectionPageParam = new Page<>(videoCollectionListDTO.getCurrent(), videoCollectionListDTO.getSize());
        Page<VideoCollection> videoCollectionPage =(Page<VideoCollection>)videoCollectionMapper.selectPage(videoCollectionPageParam , new QueryWrapper<VideoCollection>().lambda().eq(VideoCollection::getUserId, videoCollectionListDTO.getUserId()).orderByDesc(VideoCollection::getCreateDate));
        if(videoCollectionPage != null && videoCollectionPage.getRecords().size()> 0){
            ArrayList<VideoListVO> videoListVoArrayList = new ArrayList<>();
            for(VideoCollection videoCollection : videoCollectionPage.getRecords()){
                VideoListVO videoListVO = new VideoListVO();
                String videoId = videoCollection.getVideoId();
                Video video = videoMapper.selectOne(new QueryWrapper<Video>().lambda().eq(Video::getId, videoId));
                SysUser sysUser = feignSystemApi.findById(video.getCreateBy());
                BeanUtils.copyProperties(video, videoListVO);
                videoListVO.setNickname(sysUser.getNickname());
                videoListVoArrayList.add(videoListVO);
            }
            videoListVoPage.setRecords(videoListVoArrayList);
        }
        videoListVoPage.setTotal(videoCollectionPage.getTotal());
        videoListVoPage.setPages(videoCollectionPage.getPages());
        videoListVoPage.setCurrent(videoCollectionPage.getCurrent());
        videoListVoPage.setSize(videoCollectionPage.getSize());
        return videoListVoPage;
    }




}
